package com.apes.scm.contract.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.plugin.soe.metadata.BusinessAttributeValue;
import com.apes.framework.util.DateUtil;
import com.apes.scm.account.account.model.Cooperation;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * 功能：采购合同
 *
 * @author xul
 * @create 2018-04-10 9:40
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = Contract.CONST_TABLE_NAME)
public class Contract extends Model {

    public static final String CONST_TABLE_NAME = "acc_contract";

    @Id
    @Column(name = "id", length = 30)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "采购合同编码", padding = 12)
    @Comment("合同编码")
    private String id;

    @Name
    @Column(name = "name", length = 260)
    @Comment("合同名称")
    private String name;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id", nullable = false)
    @Comment("公司")
    private PartyRole company; //公司

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id"/*, nullable = false*/)
    @Comment("部门")
    private Dept dept; //部门

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "partner_id", referencedColumnName = "id", nullable = false)
    @Comment("合作伙伴（供应商或客户）")
    private PartyRole partner; //合作伙伴（供应商或客户）采购合同为供应商， 分销为单位客户

    @States(name = "合同类型", values = {
            @State(id = "supplier", name = "采购合同"),
            @State(id = "customer", name = "销售合同"),
            @State(id = "platform",name = "平台供应服务协议")})
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "contract_type", referencedColumnName = "id", nullable = false)
    private BusinessAttributeValue contractType; //合同类型(customer:销售合同, supplier:采购合同, inter:跨公司交易合同)

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "settlement_type", referencedColumnName = "id")
    @Comment("结算方式（month：月结，halfMonth：半月结，week：周结，bill：账单，totalCash：总部现金，deptCash：门店现金）")
    private Enumeration settlementType;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "cooperation_id", referencedColumnName = "id", nullable = false)
    @Comment("合作方式")
    private Cooperation cooperation; //合作方式

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "move_type", referencedColumnName = "id")
    @Comment("交货方式（direct：部分交货，one：一次性交货）")
    private Enumeration moveType; //交货方式：("direct", "部分交货"), ("one", "一次性交货");

//    @States(name = "提货方式", values = {
//            @State(id = "selfMention", name = "自提"),
//            @State(id = "delivery", name = "送货")
//            })
//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "carry_type", referencedColumnName = "id")
//    private BusinessAttributeValue carryType;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "carry_type", referencedColumnName = "id")
    @Comment("提货方式（Contract.carryType.delivery:送货, Contract.carryType.selfMention:自提）")
    private Enumeration carryType;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "parent_id", referencedColumnName = "id")
    @Comment("上级合同")
    private Contract parent; //上级合同

    @Temporal(TemporalType.DATE)
    @Column(name = "valid_from")
    @Comment("有效期起")
    private Date validFrom;

    @Temporal(TemporalType.DATE)
    @Column(name = "valid_period")
    @Comment("有效期止")
    private Date validPeriod;

    @Column(name = "state")
    @Comment("状态(create:创建,delete:删除,approve:审核,repeal:终止,extension:延期)")
    private String state;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "delete_uid", referencedColumnName = "id")
    @Comment("删除人")
    private PartyRole deleteUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "delete_date")
    @Comment("删除日期")
    private Date deleteDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_uid", referencedColumnName = "id")
    @Comment("审核人")
    private PartyRole approveUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "approve_date")
    @Comment("审核日期")
    private Date approveDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "repeal_uid", referencedColumnName = "id")
    @Comment("终止人")
    private PartyRole repealUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "repeal_date")
    @Comment("终止日期")
    private Date repealDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "extension_uid", referencedColumnName = "id")
    @Comment("延期人")
    private PartyRole extensionUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "extension_date")
    @Comment("延期日期")
    private Date extensionDate;

    @Column(name = "consign_install", columnDefinition = "number(1,0) default 1")
    private boolean consignInstall = false; //是否提供带安

    @Column(name = "origin_id", length = 12)
    @Comment("外部合同编码")
    private String originId;

    @Column(name = "system_source")
    @Comment("合同来源的系统(0:旧系统；1:新系统；2：销售业务)")
    private String systemSource;
    
}
